var express = require('express');
var router = express.Router();
const { secretKey } = require('../utils/util');
const jwt= require("jsonwebtoken");
const multiparty = require('multiparty');
const path = require('path');
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/getToken',function(req, res){
  //获取到客户端发过来的token
  const token =req.get('Authorization');
  //将编码后的token在转换回来
  const data =jwt.verify(token.split(" ")[1],secretKey);
  res.send({data:data.data});
});
router.post("/upload", function (req, res) {
  var form = new multiparty.Form({ uploadDir: "./public/images" });
  form.parse(req, function (err, fields, files) {
    if (err) {
      res.send(err);
    } else {
      console.log(files);
      var p = files.file[0].path.substring(
        files.file[0].path.indexOf("images")
      );
      res.send({filename:path.basename(p)});
    }
  });
});
module.exports = router;
